package com.ss.android.image.loader;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.NetworkType;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.common.utility.collection.WeakHandler.Callback;
import com.bytedance.frameworks.baselib.network.http.util.TaskInfo;
import com.ss.android.article.browser.R;
import com.ss.android.common.load.LRUWeakCache;
import com.ss.android.common.load.MultiAsyncLoader;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.image.BaseImageManager;
import com.ss.android.image.model.ImageInfo;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.WeakHashMap;

public class ImageLoader implements Callback {
    public final BaseImageManager mImageManager;
    final WeakHandler weakHandler = new WeakHandler(Looper.getMainLooper(), this);
    public Object PLACEHOLDER_OBJ = new Object();
    public WeakHashMap mLoadingImageViews = new WeakHashMap();
    public LRUWeakCache<String, Bitmap> mLRUWeakCache;
    public MultiAsyncLoader mMultiAsyncLoader;
    public boolean isAlive;
    private LinkedList<b> a = new LinkedList();
    private MultiAsyncLoader.LoaderProxy mLoaderProxy;
    private boolean c;
    private int maxWidth;
    private int maxHeight;
    private int placeholderResId;
    private com.bytedance.frameworks.baselib.network.http.util.d n;
    private Context mContext;
    private TaskInfo mTaskInfo;
    private Resources mResources;
    private LoadImagePolicy mLoadImagePolicy;

    public ImageLoader(Context context, TaskInfo arg12, int arg13,
                       int arg14, int arg15, BaseImageManager arg16, int arg17, int arg18) {
        this(context, arg12, arg13, arg14, arg15, arg16, arg17, arg18, 0);
    }

    public ImageLoader(Context context, TaskInfo taskInfo, int arg13,
                       int arg14, BaseImageManager baseImageManager, int arg16, int arg17) {
        this(context, taskInfo, 4, arg13, arg14, baseImageManager, arg16, arg17, R.drawable.cn);
    }

    public ImageLoader(Context context, TaskInfo taskInfo, int capacity,
                       int arg7, int arg8, BaseImageManager imageManager, int maxWidth, int maxHeight, int placeholderResId) {
        this.mContext = context.getApplicationContext();
        this.mResources = this.mContext.getResources();
        this.maxWidth = maxWidth;
        this.maxHeight = maxHeight;
        this.mTaskInfo = taskInfo;
        this.mImageManager = imageManager;
        this.mLoadImagePolicy = LoadImagePolicy.ALWAYS;
        this.placeholderResId = placeholderResId;
        this.c = true;
        if (this.maxWidth <= 0) {
            throw new IllegalArgumentException("fitWidth but maxWidth is not positive");
        }

        this.n = new com.bytedance.frameworks.baselib.network.http.util.d() {

            public final void a(int arg3, Object arg4) {
                if (arg4 != null) {
                    Message message = ImageLoader.this.weakHandler.obtainMessage(100);
                    message.arg1 = arg3;
                    message.obj = arg4;
                    ImageLoader.this.weakHandler.sendMessage(message);
                }
            }
        };
        this.mLRUWeakCache = new LRUWeakCache(capacity);
        this.mLoaderProxy = new MultiAsyncLoader.LoaderProxy<String, ImageInfo, Boolean, ImageView, ImageResult>() {
            public final ImageResult doInBackground(String key, ImageInfo param, Boolean extra) {
                return ImageLoader.this.loadImage(key, param, extra.booleanValue());
            }

            public final void onLoaded(String key, ImageInfo imageInfo, Collection<ImageView> imageViews, ImageResult result) {
                ImageLoader.this.onLoaded(key, imageInfo, imageViews, result);
            }
        };
        this.mMultiAsyncLoader = new MultiAsyncLoader(arg7, arg8, this.mLoaderProxy);
        this.isAlive = true;
    }

    private static Bitmap a(boolean arg5, int arg6, int arg7, String arg8, boolean arg9) {
        BitmapFactory.Options v3_2;
        byte[] v2_2;
        Bitmap.Config v0_3;
        int v1_2;
        Bitmap.CompressFormat v0_2;
        ByteArrayOutputStream v3_1;
        Bitmap v2_1;
        int v3;
        int v2;
        Bitmap v0_1;
        Bitmap v1 = null;
        if (!arg5) {
            if (arg9) {
                v0_3 = Bitmap.Config.ARGB_8888;
            } else {
                v0_3 = null;
            }

            v0_1 = android.support.design.a.decodeFile(arg8, 1000, arg7, v0_3);
            if (arg6 > 0) {
            } else {
                return v1;
            }
            if (v0_1 == null) {
                return v1;
            }

            try {
                v1_2 = v0_1.getWidth();
                v2 = v0_1.getHeight();
                if ((((int) ((((float) arg7)) * (((float) v1_2)) / (((float) v2))))) <= arg6) {
                    return v0_1;
                }

                v3 = ((int) ((((float) v2)) * (((float) arg6)) / (((float) arg7))));
                v1_2 = (v1_2 - v3) / 2;
                v0_1 = Bitmap.createBitmap(v0_1, v1_2, 0, v3, v2);
            } catch (Exception v1_1) {
                v1_1.printStackTrace();
            }
            return v0_1;
        }

        if (arg7 > 0) {
            v0_1 = android.support.design.a.a(arg8, arg6, arg9);
            if (v0_1 != null) {
                v1_2 = v0_1.getWidth();
                if ((((int) ((((float) v0_1.getHeight())) * (((float) arg6)) / (((float) v1_2))))) <= arg7) {
                    return v0_1;
                }
                return Bitmap.createBitmap(v0_1, 0, 0, v1_2, ((int) ((float) v1_2 * (((float) arg7)) / (float) arg6)));
            }
            return v1;
        }

        try {
            v0_1 = android.support.design.a.a(arg8, arg6, arg9);
            if (v0_1 == null) {
                return v1;
            }

            v2 = v0_1.getWidth();
            v3 = v0_1.getHeight();
            if (v2 <= arg6 + 150 && !(v2 > arg6 && v3 > 4000)) {
                v2_1 = v0_1;
            } else {
                v2_1 = Bitmap.createScaledBitmap(v0_1, arg6, ((int) ((((float) v3)) * (((float) arg6)) / (((float) v2)))), true);
                v0_1.recycle();
                if (v2_1 == null) {
                    return v1;
                } else {

                }
            }
            v3_1 = new ByteArrayOutputStream();
            if (arg9) {
                v0_2 = Bitmap.CompressFormat.PNG;
            } else {
                v0_2 = Bitmap.CompressFormat.JPEG;
            }

            v2_1.compress(v0_2, 80, v3_1);
            v2_1.recycle();
            v2_2 = v3_1.toByteArray();
            v3_1.close();
            v3_2 = new BitmapFactory.Options();
            v3_2.inPurgeable = true;
            if (arg9) {
                v0_3 = Bitmap.Config.ARGB_8888;
            } else {
                v0_3 = Bitmap.Config.RGB_565;
            }

            try {
                v3_2.inPreferredConfig = v0_3;
                v0_1 = BitmapFactory.decodeByteArray(v2_2, 0, v2_2.length, v3_2);
                return v0_1;
            } catch (Exception v0) {
                return null;
            }
        } catch (Exception v0) {
            return null;
        }
    }

    public void loadImage(ImageView iamgeView, ImageInfo imageInfo, boolean arg9) {
        String key = null;
        if ((this.isAlive) && iamgeView != null) {
            this.clear(iamgeView);
            if (imageInfo != null) {
                key = imageInfo.mKey;
            }
            iamgeView.setTag(key);
            Drawable background = iamgeView.getBackground();
            if (key == null) {
                if (background != null) {
                    background.setLevel(1);
                }
                this.a(null, iamgeView, false);
                return;
            }
            Bitmap bitmap = this.mLRUWeakCache.get(key);
            if (bitmap != null) {
                iamgeView.setImageBitmap(bitmap);
                if (background != null) {
                    background.setLevel(2);
                }
                this.a(key, iamgeView, true);
                return;
            }
            if (background != null) {
                background.setLevel(0);
            }

            if (this.placeholderResId > 0) {
                b bVar = !this.a.isEmpty() ? this.a.removeLast() : null;
                if (bVar == null) {
                    bVar = new b(this.mResources.getDrawable(this.placeholderResId));
                }
                iamgeView.setImageDrawable(bVar);
                bVar.setLevel(0);
            }
            this.mLoadingImageViews.put(iamgeView, this.PLACEHOLDER_OBJ);
            this.mMultiAsyncLoader.addTask(key, imageInfo, Boolean.valueOf(arg9), iamgeView);
        }
    }

    public final void a(LoadImagePolicy arg1) {
        if (arg1 == null) {
            arg1 = LoadImagePolicy.ALWAYS;
        }

        this.mLoadImagePolicy = arg1;
    }

    public final void clear(ImageView imageView) {
        if (imageView != null) {
            this.mLoadingImageViews.remove(imageView);
            if (this.placeholderResId > 0) {
                Drawable drawable = imageView.getDrawable();
                if ((drawable instanceof b)) {
                    this.a.add((b) drawable);
                    imageView.setImageDrawable(null);
                }
            }
        }
    }

    public void loadImage(String arg1, ImageView arg2, String arg3) {
    }

    final ImageResult loadImage(String key, ImageInfo imageInfo, boolean arg16) {
        String v2_2;
        ImageResult v0_2;
        int v1_1;
        try {
            String v12 = this.mImageManager.getExternalCacheFile(key);
            String v11 = this.mImageManager.getInternalCacheFile(key);
            File v1 = new File(v12);
            File v2 = new File(v11);
            boolean v0_1 = false;
            if ((v1.isFile()) || (v2.isFile())) {
                v0_1 = true;
            }

            v1_1 = 0;
            NetworkType networkType = NetworkUtils.getNetworkType(this.mContext);
            if (!v0_1 && ((arg16)
                    || networkType == NetworkType.WIFI
                    || networkType == NetworkType.MOBILE_4G
                    || this.mLoadImagePolicy == LoadImagePolicy.ALWAYS) && networkType != NetworkType.NONE) {
                v1_1 = 1;
            }

            if (!v0_1 && v1_1 != 0) {
                String v5 = this.mImageManager.getExternalImageSaveDir(key);
                String v6 = this.mImageManager.getInternalImageSaveDir(key);
                String v7 = BaseImageManager.getCacheFileName(key);
                com.bytedance.frameworks.baselib.network.http.util.d v8 = null;
                if (this.placeholderResId > 0) {
                    v8 = this.n;
                }
                v0_1 = com.ss.android.image.m.a().downloadImage(this.mContext, -1, imageInfo.mUri, imageInfo.mUrlList, v5, v6, v7, v8, key, this.mTaskInfo);
            }

            if (!v0_1) {
                v0_2 = null;
                return v0_2;
            }

            File v0_3 = new File(v12);
            if (!v0_3.isFile()) {
                v0_3 = new File(v11);
                v2_2 = v11;
            } else {
                v2_2 = v12;
            }

            if (!v0_3.isFile()) {
                return null;
            }

            if ((this.a()) && (imageInfo.mIsGif)) {
                return new ImageResult(v2_2);
            }

            v1_1 = this.maxWidth;
            int v0_4 = this.maxHeight;
            if (imageInfo != null && (imageInfo.isFixedDisplaySize())) {
                v1_1 = imageInfo.mWidth;
                v0_4 = imageInfo.mHeight;
            }

            Bitmap bitmap = a(this.c, v1_1, v0_4, v2_2, imageInfo.mNeedAlpha);
            ByteArrayOutputStream v2_3 = new ByteArrayOutputStream();
            Bitmap.CompressFormat v0_5 = imageInfo.mNeedAlpha ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG;
            bitmap.compress(v0_5, 80, ((OutputStream) v2_3));
            bitmap.recycle();
            byte[] v1_3 = v2_3.toByteArray();
            v2_3.close();
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPurgeable = true;
            options.inPreferredConfig = imageInfo.mNeedAlpha ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
            v0_2 = new ImageResult(BitmapFactory.decodeByteArray(v1_3, 0, v1_3.length, options));
        } catch (Throwable throwable) {
            Logger.d("ImageLoader", "loadImage exception " + throwable);
            v0_2 = null;
        }

        return v0_2;
    }

    protected boolean a() {
        return false;
    }

    public void a(String arg1, ImageView imageView, boolean arg3) {
    }

    public final void a(ImageView imageView, String uri, String urlList) {
        this.loadImage(imageView, new ImageInfo(uri, urlList), false);
    }

    final void onLoaded(String key, ImageInfo imageInfo, Collection collection, ImageResult imageResult) {
        int v0_1 = 0;
        String v1;
        Bitmap v3;
        Drawable v2 = null;
        if ((this.isAlive) && key != null && collection != null) {
            if (imageResult != null) {
                v3 = imageResult.a;
                v1 = imageResult.b;
            } else {
                v1 = null;
                v3 = null;
            }

            int v4 = !this.a() || !imageInfo.mIsGif ? 0 : 1;
            Iterator v8 = collection.iterator();
            int v7;
            for (v7 = 0; v8.hasNext(); v7 = v0_1) {
                Object v0 = v8.next();
                if (!key.equals(((ImageView) v0).getTag())) {
                    v0_1 = v7;
                } else if (v4 != 0) {
                    this.loadImage(key, ((ImageView) v0), v1);
                    continue;
                } else {
                    Drawable v7_1 = ((ImageView) v0).getBackground();
                    this.clear(((ImageView) v0));
                    if (v3 != null) {
                        ((ImageView) v0).setImageBitmap(v3);
                        if (v7_1 != null) {
                            v7_1.setLevel(2);
                        }
                    } else {
                        ((ImageView) v0).setImageDrawable(v2);
                        if (v7_1 != null) {
                            v7_1.setLevel(1);
                        }
                    }

                    boolean v7_2 = v3 != null ? true : false;
                    this.a(key, ((ImageView) v0), v7_2);
                    this.mLoadingImageViews.remove(v0);
                    v0_1 = 1;
                }
            }

            if (v4 != 0) {
                return;
            }

            if (v7 == 0) {
                return;
            }

            if (v3 == null) {
                return;
            }

            this.mLRUWeakCache.put(key, v3);
        }
    }

    public final void onPause() {
        this.isAlive = true;
        this.mMultiAsyncLoader.resume();
    }

    public final void onStop() {
        this.mMultiAsyncLoader.pause();
        this.mLRUWeakCache.shrink();
    }

    public final void onDestroy() {
        this.isAlive = false;
        this.mLoadingImageViews.clear();
        this.mMultiAsyncLoader.stop();
        LRUWeakCache lruWeakCache = this.mLRUWeakCache;
        lruWeakCache.mMap.clear();
        lruWeakCache.mHead.prev = lruWeakCache.mTail;
        lruWeakCache.mTail.next = lruWeakCache.mHead;
        if (this.mTaskInfo != null) {
            this.mTaskInfo.a = true;
        }
    }

    public void handleMsg(Message message) {
        int v2 = 99;
        if ((this.isAlive) && message.what == 100) {
            String v0 = null;
            String v1;
            if ((message.obj instanceof String)) {
                v1 = (String) message.obj;
            } else {
                v1 = null;
            }

            if (android.support.design.a.isTextEmpty((v1))) {
                return;
            }

            int v0_1 = message.arg1;
            if (v0_1 < 0) {
                v0_1 = 0;
            }

            if (v0_1 > v2) {
                v0_1 = v2;
            }

            v2 = v0_1 * 100;
            Iterator v3 = this.mLoadingImageViews.keySet().iterator();
            while (v3.hasNext()) {
                Object v0_2 = v3.next();
                if (v0_2 == null) {
                    continue;
                }

                if (!((String) v1).equals(((ImageView) v0_2).getTag())) {
                    continue;
                }

                Drawable v0_3 = ((ImageView) v0_2).getDrawable();
                if (!(v0_3 instanceof b)) {
                    continue;
                }

                v0_3.setLevel(v2);
            }
        }
    }

    final class ImageResult {
        public final Bitmap a;
        public final String b;

        public ImageResult(String arg2) {
            this.a = null;
            this.b = arg2;
        }

        public ImageResult(Bitmap arg2) {
            this.a = arg2;
            this.b = null;
        }
    }

    final class b extends Drawable {
        private Drawable a;

        public b(Drawable arg1) {
            this.a = arg1;
        }

        public final void draw(Canvas arg2) {
            if (this.a != null && this.a.getLevel() > 0) {
                this.a.draw(arg2);
            }
        }

        public final int getIntrinsicHeight() {
            int v0 = this.a != null ? this.a.getIntrinsicHeight() : 0;
            return v0;
        }

        public final int getIntrinsicWidth() {
            int v0 = this.a != null ? this.a.getIntrinsicWidth() : 0;
            return v0;
        }

        public final int getOpacity() {
            return PixelFormat.TRANSLUCENT;
        }

        protected final void onBoundsChange(Rect rect) {
            if (this.a != null) {
                int v0 = this.a.getIntrinsicWidth();
                int v1 = this.a.getIntrinsicHeight();
                int v2 = rect.width();
                int v3 = rect.height();
                if (v2 > 0 && v3 > 0 && v0 > 0 && v1 > 0) {
                    v2 = (v2 - v0) / 2 + rect.left;
                    v3 = (v3 - v1) / 2 + rect.top;
                    this.a.setBounds(v2, v3, v0 + v2, v1 + v3);
                }
            }
        }

        protected final boolean onLevelChange(int arg2) {
            boolean v0;
            if (this.a == null) {
                v0 = false;
            } else {
                this.a.setLevel(arg2);
                this.invalidateSelf();
                v0 = true;
            }

            return v0;
        }

        public final void setAlpha(int arg1) {
        }

        public final void setColorFilter(ColorFilter arg1) {
        }
    }
}

